package com.wibo.account;

import com.wibo.common.CommentItem;
import com.wibo.common.Utility;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CommentListItemView extends RelativeLayout {
	private Context mContext;
	
	private static final String TAG = "CommentListItemView";
	
	//private RelativeLayout mOriginalWeiboLayout;
	private ImageView mWeiboUserPic;
	private TextView mUserName;
	private TextView mText;
	private TextView mRootWeiboText;
	private TextView mSentTime;
	
	private EmotionsParser mParser;
	
	private CUserManager mUserManager = null;

	public CommentListItemView(Context context) {
		super(context);
		mUserManager = CUserManager.getInstance(context);
		initialize(context);
	}
	public CommentListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mUserManager = CUserManager.getInstance(context);
        initialize(context);
    }
	
	private int getListItemSkin() {
		int skin = mUserManager.getMySkin();
		switch (skin) {
		case 0:
			return R.layout.commentlistitem;
		case 1:
			return R.layout.commentlistitem_2;
		default:
			return R.layout.commentlistitem;
		}
	}
	
	private float mUserNameSize;
	private float mTextSize;
	private float mOriginalTextSize;
	private float mSentTimeSize;
//	private float mClientNameSize;
//	private float mCommentCountSize;
//	private float mTransmitCountSize;
	
	private void setTextSize() {
		int add = 0;
		switch (mUserManager.getTextSize()) {
		case CUserManager.SMALL:
			add = 0;
			mUserName.setTextSize(mUserNameSize + add);
			break;
		case CUserManager.MID:
			add = 1;
			mUserName.setTextSize(mUserNameSize + add);
			break;
		case CUserManager.LARGE:
			mUserName.setTextSize(mUserNameSize + CUserManager.MID);
			add = 2;
			break;
		default:
			break;
		}
		
		
		mText.setTextSize(mTextSize + add);
		mRootWeiboText.setTextSize(mOriginalTextSize + add);
		//mSentTime.setTextSize(mSentTimeSize + add);
		//mClientName.setTextSize(mClientNameSize + add);
		//mCommentCount.setTextSize(mCommentCountSize + add);
		//mTransmitCount.setTextSize(mTransmitCountSize + add);
	}
	
	private void initialize(Context context) {
    	this.mContext = context;
    	mParser = new EmotionsParser(mContext); 
    	
    	View view = LayoutInflater.from(this.mContext).inflate(getListItemSkin(), null);
    	
    	mWeiboUserPic = (ImageView) view.findViewById(R.id.commentUserPic);
    	
    	mUserName = (TextView) view.findViewById(R.id.commentUserName);
    	TextPaint tp = mUserName.getPaint(); 
    	tp.setFakeBoldText(true);
    	
    	mText = (TextView) view.findViewById(R.id.commentText);
    	
    	mSentTime = (TextView) view.findViewById(R.id.commentSentTime);
    	
    	mRootWeiboText = (TextView) view.findViewById(R.id.prevCommentText);
    	
    	mUserNameSize = mUserName.getTextSize();
    	mTextSize = mText.getTextSize();
    	mOriginalTextSize = mRootWeiboText.getTextSize();
    	mSentTimeSize = mSentTime.getTextSize();
//    	mClientNameSize = mClientName.getTextSize();
//    	mCommentCountSize = mCommentCount.getTextSize();
//    	mTransmitCountSize = mTransmitCount.getTextSize();
    	
    	addView(view);
	}
	
	public void updateView(CommentItem commentItem, String position) {
		setTextSize();
		Bitmap bitmap = null;
		
		if (commentItem.getWeiboUserPic() != null){
			mWeiboUserPic.setImageBitmap(Utility.getBitmap(mContext, commentItem.getWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC));
			mWeiboUserPic.setVisibility(ImageView.VISIBLE);
		}else{
			mWeiboUserPic.setVisibility(ImageView.GONE);
			mWeiboUserPic.setImageBitmap(null);
		}
		
		if (commentItem.getCreatedAt() != null) {
			mSentTime.setText(commentItem.getCreatedAt());
		}
		
		if (commentItem.getText() != null) {
			mText.setText(mParser.replace(commentItem.getText(), 18, 18));
		}
		
		if (commentItem.getUsername() != null) {
			mUserName.setText(mParser.replace(commentItem.getUsername(), 18, 18));
		}
		
		if (commentItem.getRootText() != null) {
			mRootWeiboText.setText(mParser.highlight(mParser.replace(commentItem.getRootUsername() + ": " + commentItem.getRootText(), 18, 18),
					0, commentItem.getRootUsername().length(), mContext.getResources().getColor(R.color.sky_blue)));
		}
		
		this.setTag(position);
	}
}
